package org.jeecg.modules.audit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.activity.entity.ActivityInfo;
import org.jeecg.modules.audit.entity.ShortVideoRemark;
import org.jeecg.modules.audit.service.IShortVideoRemarkService;
import org.jeecg.modules.video.entity.ShortVideoInfo;
import org.jeecg.modules.video.service.IShortVideoInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
* @Description: 短视频评论
* @Author: jeecg-boot
* @Date:   2020-05-20
* @Version: V1.0
*/
@Slf4j
@Api(tags="短视频评论")
@RestController
@RequestMapping("/video/shortVideoRemark")
public class ShortVideoRemarkController extends JeecgController<ShortVideoRemark, IShortVideoRemarkService> {
   @Autowired
   private IShortVideoRemarkService shortVideoRemarkService;

   @Autowired
   private IShortVideoInfoService shortVideoInfoService;
   /**
    * 分页列表查询
    *
    * @param shortVideoRemark
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "短视频评论-分页列表查询")
   @ApiOperation(value="短视频评论-分页列表查询", notes="短视频评论-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(ShortVideoRemark shortVideoRemark,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<ShortVideoRemark> queryWrapper = QueryGenerator.initQueryWrapper(shortVideoRemark, req.getParameterMap());
       Page<ShortVideoRemark> page = new Page<ShortVideoRemark>(pageNo, pageSize);
       IPage<ShortVideoRemark> pageList = shortVideoRemarkService.page(page, queryWrapper);
       pageList.getRecords().stream().forEach(item->{
           //获取title
           String videoId = item.getShortVideoId();
           QueryWrapper<ShortVideoInfo> videoInfoQueryWrapper = new QueryWrapper<ShortVideoInfo>();
           videoInfoQueryWrapper.eq("id",videoId).last(" limit 1");
           ShortVideoInfo shortVideoInfo = shortVideoInfoService.getOne(videoInfoQueryWrapper);
           item.setTitle(shortVideoInfo.getMainTitle());
       });

       return Result.ok(pageList);
   }

   /**
    * 添加
    *
    * @param shortVideoRemark
    * @return
    */
   @AutoLog(value = "短视频评论-添加")
   @ApiOperation(value="短视频评论-添加", notes="短视频评论-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody ShortVideoRemark shortVideoRemark) {
       shortVideoRemarkService.save(shortVideoRemark);
       return Result.ok("添加成功！");
   }

   /**
    * 审核
    *
    * @param shortVideoRemark
    * @return
    */
   @AutoLog(value = "短视频评论-审核")
   @ApiOperation(value="短视频评论-审核", notes="短视频评论-审核")
   @PutMapping(value = "/edit")
   public Result<?> edit(HttpServletRequest request,@RequestBody ShortVideoRemark shortVideoRemark) {
       String checker = JwtUtil.getUserNameByToken(request);
       shortVideoRemark.setChecker(checker);
       shortVideoRemarkService.updateById(shortVideoRemark);
       return Result.ok("编辑成功!");
   }


   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "短视频评论-通过id查询")
   @ApiOperation(value="短视频评论-通过id查询", notes="短视频评论-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       ShortVideoRemark shortVideoRemark = shortVideoRemarkService.getById(id);
       return Result.ok(shortVideoRemark);
   }


}
